﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Drawing;
using Wise.Entities;
using System.Threading;
using System.Windows.Media.Imaging;

namespace NHI.Sync.Tools.Forms
{
    public class Form_Execute_Status
    {
        public Form_Execute_Status()
        {
        }

        public Form_Execute_StatusType Status { get; set; }

        public string StatusText
        {
            get
            {
                return Status.ToString();
            }
        }

        public int Total { get; set; }

        public int Successed { get; set; }

        public int Warning { get; set; }

        public int Error { get; set; }

        void _InitOperators()
        {
            lock (Form_Execute.Current.MyOperators)
            {

                Form_Execute.Current.MyOperators.Clear();
                Form_Execute.Current.MyOperators.Add(new Form_Execute_Operator(Form_Execute_Operator.INIT_SETTING));
                Form_Execute.Current.MyOperators.Add(new Form_Execute_Operator(Form_Execute_Operator.INIT_Connection));
                Form_Execute.Current.MyOperators.Add(new Form_Execute_Operator(Form_Execute_Operator.INIT_SQL));

            }
        }

        void Start()
        {
            MyDbConfig.Current.Reset();
            MyLogger.UseNewLogger();
            try
            {
                _InitOperators();
                int count = Form_Execute.Current.MyOperators.Count;
                for (int i = 0; i < count; i++)
                {
                    count = Form_Execute.Current.MyOperators.Count;
                    Form_Execute_Operator op = null;
                    lock (Form_Execute.Current.MyOperators)
                    {
                        op = Form_Execute.Current.MyOperators[i];
                    }
                    try
                    {
                        op.Run();
                    }
                    catch (ThreadAbortException)
                    {
                        return;
                    }
                    catch (Exception exp)
                    {
                        op.Error++;
                        MyLogger.Current._错误(op.MappdingId, exp);
                        SetStatus(Form_Execute_StatusType.错误);
                        return;
                    }
                }
                SetStatus(Form_Execute_StatusType.运行成功);
            }
            catch (ThreadAbortException)
            {
                return;
            }
            catch (Exception exp)
            {
                MyLogger.Current._程序异常(exp);
                MainWindow.Current.ShowError(exp.Message);
                SetStatus(Form_Execute_StatusType.错误);
            }
        }
        //System.Threading.ThreadState _RunningStatus = System.Threading.ThreadState.Running;

        Thread _Running_Task = null;
        CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
        public void SetStatus(Form_Execute_StatusType status)
        {
            Status = status;
            try
            {
                Form_Execute.Current.Dispatcher.Invoke(() =>
                {
                    switch (status)
                    {
                        case Form_Execute_StatusType.已停止:
                            #region 已停止
                            {
                                Form_Execute.Current.btnRun.IsEnabled = true;
                                Form_Execute.Current.btnReport.IsEnabled = true;
                                Form_Execute.Current.btnStop.IsEnabled = true;
                            }
                            #endregion
                            break;
                        case Form_Execute_StatusType.运行中:
                            #region 运行中
                            {
                                Form_Execute.Current.btnRun.IsEnabled = false;
                                Form_Execute.Current.btnReport.IsEnabled = false;
                                Form_Execute.Current.btnStop.IsEnabled = true;
                            }
                            #endregion
                            break;
                        case Form_Execute_StatusType.运行成功:
                            #region 运行成功
                            {
                                Form_Execute.Current.btnStop.IsEnabled = false;
                                Form_Execute.Current.btnReport.IsEnabled = true;
                                Form_Execute.Current.btnRun.IsEnabled = true;
                            }
                            #endregion
                            break;
                        case Form_Execute_StatusType.错误:
                            #region 错误
                            {
                                Form_Execute.Current.btnStop.IsEnabled = false;
                                Form_Execute.Current.btnReport.IsEnabled = true;
                                Form_Execute.Current.btnRun.IsEnabled = true;
                            }
                            #endregion
                            break;
                    }

                });
            }
            catch (TaskCanceledException)
            {

            }

            switch (status)
            {
                case Form_Execute_StatusType.已停止:
                    #region 已停止
                    {
                        if (_Running_Task == null)
                        {
                            return;
                        }
                        else if (!_Running_Task.IsAlive)
                        //else if (_Running_Task.IsFaulted
                        //    || _Running_Task.IsCompleted
                        //    || _Running_Task.IsCanceled)
                        {
                            return;
                        }
                        else
                        {
                            cancelTokenSource.Cancel();
                            _Running_Task.Abort();
                            Form_Execute.Current._Bind_Tread.Abort();
                            MyDbConfig.Current.CloseConnections();
                        }
                    }
                    #endregion
                    break;
                case Form_Execute_StatusType.运行中:
                    #region 运行中
                    {
                        if (_Running_Task == null
                            || !_Running_Task.IsAlive
                            //|| _Running_Task.IsCanceled
                            //|| _Running_Task.IsCompleted
                            //|| _Running_Task.IsFaulted
                            )
                        {
                            _Running_Task = new Thread(Start);
                            _Running_Task.Start();
                            //_Running_Task=Task.Factory.StartNew(Start, cancelTokenSource.Token);
                            //_Running_Task= Task.Run(()=>Start());
                        }
                        //else if (_Running_Task.ThreadState.HasFlag(System.Threading.ThreadState.Suspended))
                        //{
                        //    _Running_Task.Resume();
                        //}
                        //else if (_RunningStatus == System.Threading.ThreadState.Suspended)
                        //{
                        //    _RunningStatus = System.Threading.ThreadState.Running;
                        //}
                    }
                    #endregion
                    break;
                case Form_Execute_StatusType.运行成功:
                    #region 运行成功
                    {
                        cancelTokenSource.Cancel();
                        if (_Running_Task.IsAlive)
                            _Running_Task.Abort();
                        Form_Execute.Current._Bind_Tread.Abort();
                        MyDbConfig.Current.CloseConnections();
                    }
                    #endregion
                    break;
                case Form_Execute_StatusType.错误:
                    #region 错误
                    {
                        cancelTokenSource.Cancel();
                        _Running_Task.Abort();
                        Form_Execute.Current._Bind_Tread.Abort();
                        MyDbConfig.Current.CloseConnections();
                    }
                    #endregion
                    break;
            }
        }
    }

}
